package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.ZhongBaoCustomer;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinMemberService;
import org.itboys.mobile.service.mongo.weixin.ZhongBaoWeiXinCustomerService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：江雄
 * 时间：2017年5月9日
 * 描述：众包_微信_用户_controller
 */
@Controller
@RequestMapping("/sunday/weixin/member")
public class SundayWeiXinMemberController extends BaseController{
    @Autowired
    private SundayWeiXinMemberService memberService;
    @Autowired
    private ZhongBaoWeiXinCustomerService customerService;

    @RequestMapping(value="/update", method= RequestMethod.POST)
    @ApiOperation(value="[个人信息]修改")
    public void update(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model,
            @ApiParam(required=false, name="company", value="公司名称")@RequestParam(value = "company", required = false)String company,
            @ApiParam(required=true, name="nickName", value="昵称")@RequestParam(value = "nickName", required = true)String nickName,
            @ApiParam(required=true, name="mobile", value="联系电话")@RequestParam(value = "mobile", required = true)String mobile,
            @ApiParam(required=false, name="trade", value="标签:行业")@RequestParam(value = "trade", required = false)String trade,
            @ApiParam(required=false, name="provinceId", value="标签:省ID.")@RequestParam(value = "provinceId", required = false)Long provinceId,
            @ApiParam(required=false, name="provinceName", value="标签:省名称")@RequestParam(value = "provinceName", required = false)String provinceName,
            @ApiParam(required=false, name="cityId", value="标签:市ID")@RequestParam(value = "cityId", required = false)Long cityId,
            @ApiParam(required=false, name="cityName", value="标签:市名称")@RequestParam(value = "cityName", required = false)String cityName,
            @ApiParam(required=false, name="name1", value="标签:名称1")@RequestParam(value = "name1", required = false)String name1,
            @ApiParam(required=false, name="name2", value="标签:名称2")@RequestParam(value = "name2", required = false)String name2,
            @ApiParam(required=false, name="name3", value="标签:名称3")@RequestParam(value = "name3", required = false)String name3
    ){
        try{

            memberService.updateDetail(
                    AccountIdHoldUtils.getSessionMember(request).getId(),company, nickName, mobile,
                    //标签的信息
                    trade,  provinceId,  provinceName,  cityId,  cityName,  name1,  name2,  name3);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/member/update----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**
     * 新增项目
     * @param request
     * @param response
     * @param
     */
    @RequestMapping(value="/getMembers", method=RequestMethod.POST)
    @ApiOperation(value="获取用户列表")
    public void getMembers(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=false, name="userType", value="用户类型")@RequestParam(value = "userType", required = false)Integer userType,
            @ApiParam(required=false, name="parentId", value="上级ID")@RequestParam(value = "parentId", required = false)Long parentId,
            @ApiParam(required=false, name="nickName", value="昵称")@RequestParam(value = "nickName", required = false)String nickName,
            @ApiParam(required=false, name="memberIds", value="用户集合")@RequestParam(value = "memberIds", required = false)List<Long> memberIds,
            @ApiParam(required=false, name="notMemberIds", value="排除的用户集合")@RequestParam(value = "memberIds", required = false)List<Long> notMemberIds

            ){
        try {
           List<SundayMember> memebrs =  memberService.getMembers(request,userType,parentId,nickName,memberIds,notMemberIds);
            ResponseMessage.success(memebrs);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/sunday/weixin/member/getMembers--------"+e);
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }

    /**
     * 员工首页，不分页获取员工
     * @param request
     * @param response
     * @param model
     * @return
     */
    /* @RequestMapping(value="/employee/index", method= RequestMethod.GET)
    @ApiOperation(value="[员工]首页。不分页获取员工")
    public String employee_index(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model
    ){
        try{

            List<SundayMember>  employees= memberService.getEmployee(request,AccountIdHoldUtils.getSessionMember(request).getId());
            model.addAttribute("employees",employees);
            return "/zhongbao/weixin/home/member/employee/index";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/member/employee/index----e="+e.getMessage());
            }
            e.printStackTrace();
            return "/zhongbao/weixin/error";
        }
    }

   @RequestMapping(value="/employee/input", method= RequestMethod.GET)
    @ApiOperation(value="[员工]员工新增/修改页")
    public String employee_input(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model,
            @ApiParam(required=true, name="employeeId", value="员工ID")@RequestParam(value = "employeeId", required = true)Long employeeId
    ){
        try{

         SundayMember employee= memberService.getDetail(employeeId);
            model.addAttribute("employee",employee);
            return "/zhongbao/weixin/home/member/employee/input";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/member/employee/input----e="+e.getMessage());
            }
            e.printStackTrace();
            return "/zhongbao/weixin/error";
        }
    }

    *//**
     * 员工新增或修改页
     * @param request
     * @param response
     * @param model
     * @param employeeId
     * @param nickName
     * @param mobile
     * @param trade
     * @param provinceId
     * @param provinceName
     * @param cityId
     * @param cityName
     * @param name1
     * @param name2
     * @param name3
     * @return
     *//*
    @RequestMapping(value="/employee/save", method= RequestMethod.POST)
    @ApiOperation(value="[员工]员工新增/修改页")
    public void employee_save(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model,
            @ApiParam(required=true, name="employeeId", value="员工ID")@RequestParam(value = "employeeId", required = true)Integer employeeId,
            @ApiParam(required=true, name="nickName", value="昵称")@RequestParam(value = "nickName", required = true)String nickName,
            @ApiParam(required=true, name="mobile", value="联系电话")@RequestParam(value = "mobile", required = true)String mobile,



            @ApiParam(required=false, name="trade", value="标签:行业")@RequestParam(value = "trade", required = false)String trade,
            @ApiParam(required=false, name="provinceId", value="标签:省ID.")@RequestParam(value = "provinceId", required = false)Long provinceId,
            @ApiParam(required=false, name="provinceName", value="标签:省名称")@RequestParam(value = "provinceName", required = false)String provinceName,
            @ApiParam(required=false, name="cityId", value="标签:市ID")@RequestParam(value = "cityId", required = false)Long cityId,
            @ApiParam(required=false, name="cityName", value="标签:市名称")@RequestParam(value = "cityName", required = false)String cityName,
            @ApiParam(required=false, name="name1", value="标签:名称1")@RequestParam(value = "name1", required = false)String name1,
            @ApiParam(required=false, name="name2", value="标签:名称2")@RequestParam(value = "name2", required = false)String name2,
            @ApiParam(required=false, name="name3", value="标签:名称3")@RequestParam(value = "name3", required = false)String name3
            ){
        try{

             memberService.saveEmployee(
                    employeeId, nickName, mobile, AccountIdHoldUtils.getSessionMember(request).getId(),
                    //标签的信息
                    trade,  provinceId,  provinceName,  cityId,  cityName,  name1,  name2,  name3);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/member/employee/save----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }*/
 /*   @RequestMapping(value="/contact/index", method= RequestMethod.GET)
    @ApiOperation(value="[通讯录]首页。不分页获取客户和同事")
    public String contact_index(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model){
        try{
            SundayMemberSessionDto memberSessionDto=AccountIdHoldUtils.getSessionMember(request);
            //客户。
            List<SundayMember> customers = customerService.getCustomer(request,memberSessionDto.getId());
            model.addAttribute("customers",customers);
            //同事
            List<SundayMember> classMates = memberService.getEmployee(request,memberSessionDto.getId());
            model.addAttribute("classMates",classMates);
            return "/zhongbao/weixin/home/member/contact/index";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/member/contact/index----e="+e.getMessage());
            }
            e.printStackTrace();
            return "/zhongbao/weixin/error";
        }
    }*/
    /**
     * 新增项目
     * @param request
     * @param response
     * @param
     */
    @RequestMapping(value="/getEmployee", method=RequestMethod.POST)
    @ApiOperation(value="获取员工或同事列表")
    public void getEmployee(
            HttpServletRequest request,
            HttpServletResponse response

    ){
        try {
            List<SundayMember> members =  memberService.getEmployee(request,AccountIdHoldUtils.getSessionMember(request).getId());
            ResponseMessage.success(members);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/sunday/weixin/member/getMembers--------"+e);
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
}
